package com.example.war;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;

// Fires
public class Fires {
    
    private Context mContext;
    private long mLastDrawTime;
    private int mCurrentIndex = 0;
    private static final int NUM_FRAMES = 33;
    private Quad[] mFrames = new Quad[NUM_FRAMES];
    private static final int INTERVAL_TIME = 50;
    private static final int RES_IDS[] = {
//        R.raw.huo0, R.raw.huo1, R.raw.huo2, R.raw.huo3, R.raw.huo4,
//        R.raw.huo5, R.raw.huo6, R.raw.huo7, R.raw.huo8,
//        R.raw.huo9, R.raw.huo10, R.raw.huo11, R.raw.huo12,
//        R.raw.huo13, R.raw.huo14, R.raw.huo15, R.raw.huo16,
//        R.raw.huo17, R.raw.huo18, R.raw.huo19, R.raw.huo20,
//        R.raw.huo21, R.raw.huo22, R.raw.huo23, R.raw.huo24,
//        R.raw.huo25, R.raw.huo26, R.raw.huo27, R.raw.huo28,
//        R.raw.huo29, R.raw.huo30, R.raw.huo31, R.raw.huo32
    };
    
    private int mIntervalAdjust = 0;
    private Point mScrSize;

    public Fires(Context context, GL10 gl) {
        mContext = context;
        for (int i = 0; i < mFrames.length; i++) {
            mFrames[i] = new Quad(false);
            initQuad(gl, mFrames[i], RES_IDS[i]);
        }
        mScrSize = Utils.getScreenSize(context);
    }

    private void initQuad(GL10 gl, Quad quad, int resId) {
        Bitmap bitmap = Utils.loadRawImage(mContext.getResources(), resId);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        quad.setSize(width*1.5f, height*1.5f);
        quad.setTexture(gl, bitmap, false);
        bitmap.recycle();
    }

    public void draw(GL10 gl) {
        long currentTime = System.currentTimeMillis();
//        long elapseTime = currentTime - mStartTime;
//        if (elapseTime > 0 && elapseTime < mAnimDuration) {
//            mCurrentIndex = (int)(elapseTime / mFrameInterval);
//            gl.glPushMatrix();
//            gl.glRotatef(180, 0, 0, 1);
//            mFrames[mCurrentIndex].draw(gl, 0, 0, 0, 0, true);
//            gl.glPopMatrix();
//        }
        
        if (mLastDrawTime == 0 || currentTime - mLastDrawTime > (INTERVAL_TIME + mIntervalAdjust)) {
            mCurrentIndex++;
            if (mCurrentIndex >= NUM_FRAMES) {
                mCurrentIndex = 1;
                mIntervalAdjust = 0;
            } else {
                mIntervalAdjust = 0;
            }
            mLastDrawTime = System.currentTimeMillis();
        }
        gl.glPushMatrix();
        Quad quad = mFrames[mCurrentIndex];
        float yPos = -(mScrSize.y - quad.mHeight)/2.0f;
        quad.draw(gl, 0, 0, yPos, 0, true);
        gl.glPopMatrix();
    }
}
